using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using IQIGame.Onigao.GameAOT;
using System.Collections.Generic;
namespace IQIGame.Onigao.GamePlay
{
    public class UI_FriendListMould : UIBaseNode
    {
        #region Template Generate,don't modify
        protected class UIB_UI_FriendListModule
        {
            #region ObjectBinding Generate
            public IQIGame.Onigao.Framework.ExText name { protected set; get; }
            public IQIGame.Onigao.Framework.ExText online { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton btnBlock { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton btnChat { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton btnRemarkName { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton btnDeleteFriend { protected set; get; }
            public IQIGame.Onigao.Framework.ExImage userImage { protected set; get; }
            public IQIGame.Onigao.Framework.ExText level { protected set; get; }
            public UnityEngine.GameObject commonPlayerItem { protected set; get; }
            public UnityEngine.RectTransform playerCoatOfArms { protected set; get; }
            public IQIGame.Onigao.Framework.ExText txtBlock { protected set; get; }
            public IQIGame.Onigao.Framework.ExText remark { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("name", out var __tbv0);
                this.name = __tbv0;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("online", out var __tbv1);
                this.online = __tbv1;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnBlock", out var __tbv2);
                this.btnBlock = __tbv2;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnChat", out var __tbv3);
                this.btnChat = __tbv3;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnRemarkName", out var __tbv4);
                this.btnRemarkName = __tbv4;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnDeleteFriend", out var __tbv5);
                this.btnDeleteFriend = __tbv5;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExImage>("userImage", out var __tbv6);
                this.userImage = __tbv6;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("level", out var __tbv7);
                this.level = __tbv7;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("commonPlayerItem", out var __tbv8);
                this.commonPlayerItem = __tbv8;
                __binding.TryGetVariableValue<UnityEngine.RectTransform>("playerCoatOfArms", out var __tbv9);
                this.playerCoatOfArms = __tbv9;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("txtBlock", out var __tbv10);
                this.txtBlock = __tbv10;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("remark", out var __tbv11);
                this.remark = __tbv11;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region fields
        private UIN_CommonPlayerItem playerItem = null;
        bool isBlocked = false;

        #endregion fields

        #region properties

        protected UIB_UI_FriendListModule ui { get; set; }
        protected UIMsgDispatcher uiMsgDispatcher => _msgDispatcher as UIMsgDispatcher;
        public FriendData friendData { get; private set; }
        #endregion properties

        protected override void BeforeInit()
        {
            ui = new UIB_UI_FriendListModule();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            playerItem = this.InitChildNode<UIN_CommonPlayerItem>(this.ui.commonPlayerItem);
        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {

        }

        /// <summary>
        /// 添加UGUI侦听
        /// </summary>
        protected override void AddUGUIListener()
        {

            this.ui.btnChat.AddClickEvent(OnClickChat);
            this.ui.btnDeleteFriend.AddClickEvent(OnClickDeleteFriend);
            this.ui.btnRemarkName.AddClickEvent(OnClickRemarkName);
            this.ui.btnBlock.AddClickEvent(OnClickBlock);
        }


        /// <summary>
        /// 添加全局侦听（MsgDispatcher）
        /// </summary>
        protected override void AddMsgListeners()
        {

        }

        protected override void OnDispose()
        {

        }

        #region methonds

        public void Show(FriendData data)
        {
            this.friendData = data;
            this.ui.name.text = this.friendData.playerName;

            this.playerItem.SetSelf(this.friendData.avatar, this.friendData.avatarFrame, this.friendData.level);
            this.playerItem.Show();
            this.ui.remark.text = this.friendData.remark;
            if (FriendModule.Instance.IsBlocked(data.pid))
            {
                this.ui.txtBlock.text = "取消屏蔽";
                isBlocked = true;
            }
            else
            {
                this.ui.txtBlock.text = "屏蔽";
                isBlocked = false;
            }
            /*称号预设方式
            var CoatOfArms = this.resLoader.LoadABAsset<GameObject>(TableCenter.playerCoatOfArms.Get(friendData.title).Prefab_fullPath);
            GameObject.Instantiate(CoatOfArms, this.ui.playerCoatOfArms);
            */
            //称号图片方式
            var img = this.ui.playerCoatOfArms.GetComponentInChildren<ExImage>();
            if (friendData.title != 0) SetImageSprite(img, TableCenter.playerCoatOfArms.Get(friendData.title).Prefab_fullPath);

            FriendModuleExtensions.SetOnline(this.ui.online, this.friendData);

        }


        private void OnClickRemarkName()
        {
            this.OpenUI<UIRemarkSet>(UD_RemarkSet.Create(friendData.id));
        }

        private void OnClickDeleteFriend()
        {
            var list = new List<long>();
            list.Add(friendData.id);
            FriendModule.NetCore.CS_removeFriends(list);
        }

        private void OnClickChat()
        {

        }

        private void OnClickBlock()
        {
            if (!isBlocked)
            {

                FriendModule.NetCore.CS_addBlacklist(friendData.pid);
            }
            else
            {
                var list = new List<long>();
                var dic = FriendModule.Instance.GetShieldListData();
                foreach (var item in dic)
                {
                    if (item.Value.pid == friendData.pid) list.Add(item.Value.id);
                }
                FriendModule.NetCore.CS_removeBlacklist(list);

            }
        }


        #endregion methonds
    }
}